package com.powerunion.datacollection.domain;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.BsResponse;
import com.powerunion.datacollection.common.Constants;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.SubmissionWorkItemDao;
import com.powerunion.datacollection.model.ApproveActivityInstance;
import com.powerunion.datacollection.model.query.SubmissionParam;
import com.powerunion.datacollection.service.ActivityManager;
import com.powerunion.datacollection.service.ApproveWorkItemManager;
import com.powerunion.datacollection.service.SubmissionWorkItemManager;
import com.powerunion.util.log.LogHelper;
/**
 * 顺序并行报送模式
 * 报送的各个步骤按照流程预先设置的运转依次执行
 * @author qyw
 *
 */
public class ProcessSequenceSumission implements IProcessSubmissionStrategy {
	private static Logger logger = LogHelper.getInstance().getLogger(
			ProcessSequenceSumission.class);
	private SubmissionWorkItemDao dao = null;
	
	public ProcessSequenceSumission(){
		super();
		this.dao = (SubmissionWorkItemDao) DaoFactory.getInstance().getDao(
				SubmissionWorkItemDao.class);
	}
	
	public BsResponse submission(SubmissionParam param) {
		//参数为空
		if(param == null) {
			String[] args = { "param" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter param NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		String result = "";
		boolean flag = false;
		//判断该任务是否已经完成或被隐藏
		flag = this.dao.iSubWorkCommit(submissionWorkId);
		if(flag){//该任务是否被隐藏或已经完成
			result = "该任务已经结束！不能提交！";
			return result;
		}
		
		//需要判断环节类型来根据规则是否提交该上报数据
		boolean ipermit = this.dao.submissionRule(actId);
		if(ipermit){//允许
			int subType = this.getActivityType(actId);
			String subWorkIds = "";
			if(subType == Constants.SUBMISSION_TYPE_MULTI){//多人类型，则保存所有的报送任务id
				subWorkIds = this.dao.getActAllSubWorkIds(actId); 
			}else if(subType == Constants.SUBMISSION_TYPE_SINGLE){//单人类型，则只保存当前提交的报送任务id
				subWorkIds = ";"+submissionWorkId+";";//数据库数据格式为";id;"
			}
			ActivityManager am = new ActivityManager();
			//根据提交数据的采集步骤id得到上级的审批步骤
			ApproveActivityInstance parentActivity = am.getParentActivity(actId);
			ApproveActivityInstance aai = am.getApproveActivityInstance(nextActivityId);

			aai.setSubmiUser(subWorkIds);
			aai.setPreActivityId(actId);
			aai.setPreActivityType(Constants.COLLECTION_STEP_TYPE);
			
			ApproveWorkItemManager aim = new ApproveWorkItemManager();
			//如果之前已经有审批过 属于是要求被重新填写再提交的，则把先前的审批任务隐藏，重新生成新的任务
			aim.hideAllApproveWork(actId);

			flag = aim.initApproveWork(aai,formId);
			
			if(flag){//提交成功，则更新采集步骤状态
				if(subType == Constants.SUBMISSION_TYPE_SINGLE){
					//如果该采集步骤是单人类型而有设了多个采集人，又因为只会有一个人提交数据，所以把该采集步骤的其他非提交人的任务隐藏掉
					this.dao.updateState(Constants.SUBMISSION_HIDE, actId);
					//然后设置当前这个提交人的状态为等待审批
					flag = this.dao.updateState4User(Constants.SUBMISSION_WAIT4Approve, submissionWorkId);
				}else{
					flag = this.dao.updateState(Constants.SUBMISSION_WAIT4Approve, actId);
				}
				 //更新流程实例中该步骤的状态
				ActivityManager activityManager = new ActivityManager();
				if(!parentActivity.getActivityId().equals(aai.getActivityId())){
					activityManager.updateActivityState(Constants.ACTIVITY_JUMP, parentActivity.getActivityId());
					activityManager.updateActivityState(Constants.ACTIVITY_JUMP, actId);
				}else{
					activityManager.updateActivityState(Constants.ACTIVITY_WAIT, actId);
				}
				
				result = "";
			}else{
				result = "提交数据失败！";
			}
		}else{
			int acivityType = this.dao.getActivityType(actId);
			if(acivityType == Constants.SUBMISSION_TYPE_SINGLE){
				result = "其他人已经提交了该采集数据！";
			}else if(acivityType ==  Constants.SUBMISSION_TYPE_MULTI){
				flag = this.dao.updateState4User(Constants.SUBMISSION_SYNCHRO_WAIT, submissionWorkId);
				result = "";
			}
		}
	
		return result;
	}

}
